﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Adams.Public.Api.Controllers.Web.Areas.Membership
{
    [EnsureHttps]
    public class ClassifiedsController : Controller
    {
        public ActionResult CreatePageLoad(Adams.Public.DataTransfer.Web.Areas.Membership.Classifieds.Create.PageLoadInput input)
        {
            input.MemberID = Api.Security.GetMemberIDFromToken(input.Token);
            var model = DataAccess.Web.Areas.Membership.Classifieds.Create.PageLoad(input);
            return new JsonXmlResult(model);
        }

        public ActionResult GetCategoriesByType(Adams.Public.DataTransfer.Web.Areas.Membership.Classifieds.Create.GetCategoriesByTypeInput input)
        {
            var model = DataAccess.Web.Areas.Membership.Classifieds.Create.GetCategoriesByType(input);
            return new JsonXmlResult(model);
        }

        public ActionResult Save(DataTransfer.Web.Areas.Membership.Classifieds.Create.FormInput input)
        {
            input.MemberID = Api.Security.GetMemberIDFromToken(input.Token);
            var output = Validation.PerformBasicValidation(input);

            if (output.IsValid)
            {
                var save = new DataTransfer.Web.Areas.Membership.Classifieds.Create.SaveInput();
                Shared.ObjectHelper.CopyObject(input, ref save);

                save.Status = Shared.Entities.Classifieds.Status.PendingApproval;

                if (input.ID.ToInt() == 0)
                {    
                    save.ExpiresOn = DateTime.Now.AddDays(14);
                }
                else
                {
                    save.ExpiresOn = DateTime.Now; //ignored
                }

                DataAccess.Web.Areas.Membership.Classifieds.Create.Save(save);
                output.IsSaved = true;
            }

            return new JsonXmlResult(output);
        }
    }
}
